package cn.lanqiao.demo.service.impl;


import cn.lanqiao.demo.dao.StudentDao;
import cn.lanqiao.demo.dao.TeacherDao;
import cn.lanqiao.demo.dao.UserDao;
import cn.lanqiao.demo.entity.Student;
import cn.lanqiao.demo.entity.Teacher;
import cn.lanqiao.demo.entity.User;
import cn.lanqiao.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    private TeacherDao teacherDao;
    private StudentDao studentDao;
    //登录
    @Override
    public List<Map> login(String username , String password, Integer user_type) {
        System.out.println(user_type+"===="+username+"===="+password);
        List<Map> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        String s_username = null;
        if(user_type==1){//学生登录
            s_username="S"+username;
        }else if(user_type==2){//教师登录
            s_username="T"+username;
        }else if(user_type==3){//管理员登录
            s_username=username;
        }
        System.out.println(s_username+"===="+password);
        User a = userDao.findUser(s_username);
        if (a != null) {
            User user = new User();
            user.setUsername(s_username);
            user.setPassword(password);
            User u = userDao.login(user);
            if (u != null) {
                //登录成功
                map.put("msg","ok");
                map.put("username",s_username);
                map.put("user_type",user_type);
            } else {
                //密码错误
                map.put("msg","error");
            }
        } else {
            //用户不存在
            map.put("msg","no_user");
        }
        list.add(map); //将Map存入List中
        return list;
    }
    //注册
    @Override
    public String addUser(String username,String password,Integer user_type) {
        String s_username = null;
        if(user_type==1){
            s_username = "S"+username;
            User a = userDao.findUser(s_username);
            if (a == null){

                User user = new User();
                user.setUsername(s_username);
                user.setPassword(password);

                Student student = new Student();
                student.setSno(s_username);

                int t = userDao.addUser(user);
                int stu = studentDao.addStudent(student);

                if(t>0 && stu>0){
                    //注册成功
                    return "add_ok";
                }else{
                    //注册shibai
                    return "add_no";
                }
            }else{
                //用户已存在
                return "user_exist";
            }
        }else if(user_type==2){
            s_username = "T"+username;
            User a = userDao.findUser(s_username);
            if (a == null){

                User user = new User();
                user.setUsername(s_username);
                user.setPassword(password);

                Teacher teacher = new Teacher();
                teacher.setTno(s_username);

                int t = userDao.addUser(user);
                int tea = teacherDao.insert(teacher);

                if(t>0 && tea>0){
                    //注册成功
                    return "add_ok";
                }else{
                    //注册shibai
                    return "add_no";
                }
            }else{
                //用户已存在
                return "user_exist";
            }
        }
        return "";

    }

    @Override
    public Integer updateUserPassword(String username,String password,String newPwd) {
        //查找用户是否已存在
        User u = userDao.findUser(username);
        if(u != null){//若存在，修改用户密码
            return userDao.updateUserPassword(username,password,newPwd);
        }else{//不存在，返回null
            return null;
        }
    }

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Autowired
    public void setTeacherDao(TeacherDao teacherDao) {
        this.teacherDao = teacherDao;
    }
    @Autowired
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
}
